Scroll Progress Bar

Modulus Operator

The modulus operator, denoted by the symbol %, is used to find the remainder of the division of two numbers. It is a fundamental arithmetic operator and is often used in programming to perform tasks like checking divisibility and finding periodic repetitions.

Syntax:

remainder = dividend % divisor;
dividend: The number to be divided.
divisor: The number by which the dividend is divided.
remainder: The result of the modulus operation, which represents the remainder of the division.
Example Program:

Let's create a C program that takes two integers as input and calculates the remainder (modulus) when the first number is divided by the second number.


#include <stdio.h>

int main() {
    int dividend, divisor, remainder;

    printf("Enter the dividend: ");
    scanf("%d", ÷nd);

    printf("Enter the divisor: ");
    scanf("%d", &divisor);

    // Calculate the remainder using the modulus operator
    remainder = dividend % divisor;

    printf("The remainder of %d divided by %d is %d\n", dividend, divisor, remainder);

    return 0;
}
Explanation:

In this program, use the modulus operator to find the remainder when the dividend is divided by the divisor. The program takes the values of dividend and divisor as input from the user. It then calculates the remainder using the % operator and stores the result in the variable remainder. Finally, the program prints the value of remainder along with the original dividend and divisor.


Example Output:
Enter the dividend: 17 Enter the divisor: 5 The remainder of 17 divided by 5 is 2 In this example, 17 divided by 5 is 3 with a remainder of 2. The modulus operation correctly calculates the remainder as 2. Modulus is particularly useful when dealing with cyclic behavior, such as finding the day of the ek based on the number of days passed, determining odd/even numbers, or working with repetitive patterns.

What is the modulus operator used for in C?


Remainder

What symbol represents the modulus operator in C?


Percent

What type of values does the modulus operator work with in C?


Integers